<template>
  <div>
    <div class="top">
      <el-form :inline="true" style="margin-top: 10px">
        <el-form-item label="选择支付平台" class="mt-form-item-left">
          <el-select v-model="payment_company" clearable filterable>
            <el-option
              v-for="(item,index) in payment_companys"
              :key="index"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-upload
            class="upload-demo"
            :action="upload_url"
            :on-remove="handleRemove"
            :data="{ payment_company:payment_company }"
            :on-exceed="handleExceed"
            accept=".xlsx, .xls"
            :on-success="handleSuccess"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>

    </div>
    <div v-loading="listLoading">
      <el-row style="margin-left: 50px;">
        trakcing文件生成完成: <el-link v-if="download_link" :href="download_link">点击下载</el-link>
      </el-row>
      <el-row v-if="data.failed.length > 0" style="margin-left: 50px;margin-top: 50px">
        <h2>获取失败列表</h2>
        <el-card v-for=" (item,index) in data.failed" :key="index" class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ item.website }}</span>
          </div>
          <div v-for=" (o,n) in item.values" :key="n" class="text item">
            {{ o }}
          </div>
        </el-card>
      </el-row>
    </div>

  </div>
</template>

<script>
export default {
  name: 'Trackings',
  data() {
    return {
      listObj: {},
      fileList: [],
      upload_url: '',
      payment_company: 'wintopay',
      width: '80%',
      pagesize: 10,
      page: 1,
      total: 0,
      kw: '',
      download_link: '',
      data: { 'download_link': '', failed: [] },
      payment_companys: [
        {
          'name': 'WinToPay',
          'value': 'wintopay'
        },
        {
          'name': 'PayPal',
          'value': 'paypal'
        },
        {
          'name': 'pacypay',
          'value': 'pacypay'
        }
      ],
      title: '产品销售情况',
      total_sales: 0,
      total_orders: 0,
      order_sales: 0,
      daily_sales: 0,
      dialogVisible: false,
      listLoading: false,
      lineChartData: {},
      timerange: ''
    }
  },
  mounted() {
    this.boolUploadSucceed = false
    this.upload_url = process.env.VUE_APP_BASE_API + '/orders/api/trackings/'
  },
  methods: {
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    handleSuccess(response, file) {
      if (response.success) {
        this.$notify({
          title: '成功',
          message: `文件${file.name}上传成功`,
          type: 'success',
          duration: 1000
        })
        this.download_link = response.data.download_link
        this.data = response.data
        console.log(this.data)
      } else {
        this.$notify({
          title: '失败',
          message: `文件${file.name}上传失败, ${response.msg}`,
          type: 'error',
          duration: 3000
        })
      }
    },
    handleRemove(file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]]
          return
        }
      }
    },
    beforeUpload(file) {
      const _self = this
      const fileName = file.uid
      this.listObj[fileName] = {}
      return new Promise((resolve, reject) => {
        _self.listObj[fileName] = { hasSuccess: false, uid: file.uid }
        resolve(true)
      })
    },
    handleModify(row) {
      this.innerDialogVisible = true
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    }
  }

}
</script>

<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}

.top >>> .el-input {
  width: auto;
}

.top >>> .input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.top {
  padding-left: 20px;
  margin-bottom: 5px;
  margin-top: 28px;
}

.input-with-select {
  width: calc(100% - 200px);
}

.add-btn {
  float: right;
  margin-right: 20px;
  width: 150px
}

.mt-pagination {
  float: right;
  margin-top: 5px;
}

.app-container >>> .el-dialog {
  margin-top: 48px !important;
  margin-bottom: 5px;
}

.mt-dialog >>> .el-transfer-panel {
  width: calc(50% - 60px);
}

.mt-form >>> .el-select {
  width: 100%;
}

.mt-form >>> .el-input__inner {
  width: 100%;
}

.mt-dialog >>> .el-dialog {
  margin: 10px auto !important;
}

.mt-dialog >>> .el-dialog__body {
  padding-bottom: 60px;
  padding-top: 5px;
}

.top >>> .el-select {
  width: 300px;
}

.mt-form >>> a:visited {
  color: purple;
}

.mt-form >>> a:link {
  color: #409EFF;
}

.mt-form >>> a:active {
  color: red;
}

.mt-form >>> a:hover {
  color: red;
  text-decoration: underline;
}
</style>
